#include "StdAfx.h"
#include "LPFactory.h"

#include "LPWorkShop.h"
#include "LPRawMaterialWareHouse.h"
#include "LPProductWareHouse.h"
#include "ConstraintParam.h"

CLPFactory::CLPFactory(CConstraintParam* pConstraintParam, std::vector<std::vector<CLPModelDecVarFactor>>& vDecVarArr)
	: m_pConstraintParam(pConstraintParam)
	, m_vDecVarArr(vDecVarArr)
	, m_pLPWorkShop(NULL)
	, m_pLPRawMaterialWareHouse(NULL)
{
	m_pLPWorkShop = new CLPWorkShop();
	m_pLPRawMaterialWareHouse = new CLPRawMaterialWareHouse;
}

CLPFactory::~CLPFactory(void)
{
	if (m_pLPWorkShop != NULL)
	{
		delete m_pLPWorkShop;
		m_pLPWorkShop = NULL;
	}

	if (m_pLPRawMaterialWareHouse != NULL)
	{
		delete m_pLPRawMaterialWareHouse;
		m_pLPRawMaterialWareHouse = NULL;
	}
}

bool CLPFactory::BuildRawMaterialWareHouse()
{
	if (NULL == m_pConstraintParam || m_vDecVarArr.empty() || NULL == m_pLPRawMaterialWareHouse)
		return false;

	m_pLPRawMaterialWareHouse->StoreMaterial(m_vDecVarArr);

	return true;
}

bool CLPFactory::Produce()
{
	if (NULL == m_pConstraintParam || m_vDecVarArr.empty() ||
		NULL == m_pLPWorkShop || NULL == m_pLPRawMaterialWareHouse)
		return false;

	std::vector<std::vector<CLPModelDecVarFactor>> vCurDecVarArr;

	int iCurMintTime = 1;
	do 
	{
		m_pLPRawMaterialWareHouse->ExtractMaterial(vCurDecVarArr);
		m_pLPWorkShop->SetMaterial(m_pConstraintParam, vCurDecVarArr);
		m_pLPWorkShop->Produce();
		std::vector<std::vector<CLPModelDecVarFactor>> vQualifiedDecVarArr;
		std::vector<std::vector<CLPModelDecVarFactor>> vUnQualifiedDecVarArr;
		m_pLPWorkShop->GetProduct(vQualifiedDecVarArr, vUnQualifiedDecVarArr);

		CLPProductWareHouse productWareHouse;
		productWareHouse.SetCapacity((int)m_vDecVarArr.size());
		productWareHouse.AddProduct(vQualifiedDecVarArr);
		m_vLPProductWareHouse.push_back(productWareHouse);

		m_pLPRawMaterialWareHouse->StoreMaterial(vUnQualifiedDecVarArr);

		iCurMintTime++;
	} while(iCurMintTime <= m_pConstraintParam->GetTotalPeriod());

	return true;
}


